package com.dxy.netty.tcppackagesolve;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @Description 协议消息编码器
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2022年09月10日
 */
public class ProtocolMessageEncoder extends MessageToByteEncoder<ProtocolMessage> {
    @Override
    protected void encode(ChannelHandlerContext ctx, ProtocolMessage msg, ByteBuf out) throws Exception {
        System.out.println("ProtocolMessageEncoder.encode() 被调用");
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getContent());
        //这里最后修改使用\r\n
        out.writeBytes(new byte[]{'\r','\n'});
    }
}
